package com.dc.designPattern._16_observer.impl;

import com.google.common.collect.Lists;

import java.util.List;

/**
 * Description: 观察者模式_具体订阅者_气象数据
 * @author: gdc
 * @date: 2021/5/20
 * @version 1.0
 */
public class WeatherData implements Subject {

    /**
     * 观察者列表
     */
    private List<Observer> observerList;

    /**
     * 温度
     */
    private double temperature;
    /**
     * 压力
     */
    private double pressure;
    /**
     * 湿度
     */
    private double humidity;

    public WeatherData() {
        this.observerList = Lists.newArrayList();
    }

    @Override
    public void register(Observer observer) {
        observerList.add(observer);
    }

    @Override
    public void remove(Observer observer) {
        observerList.remove(observer);
    }

    @Override
    public void notifyObservers() {
        observerList.forEach(observer -> observer.update(this.temperature, this.pressure, this.humidity));
    }

    /**
     * 设置数据
     *
     * @param temperature 温度
     * @param pressure    压力
     * @param humidity    湿度
     */
    public void setData (double temperature, double pressure, double humidity) {
        this.temperature = temperature;
        this.pressure = pressure;
        this.humidity = humidity;
    }

    /**
     * 修改数据
     *
     */
    public void dataChange() {
        notifyObservers();
    }

}
